﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileObject;
using CalculateModelClasses;
using SetupFileObject;
using System.IO;
using RayTracingModel;
using TerFileProceed;
using UanFileProceed;

namespace TxRxFileProceed
{
    public class TxFileProceed
    {
        //path为Tx文件路径,setup为setup文件路径，terpath为ter文件路径
        #region
        private static List<TxObject> TxReader(string path)
        {
            List<TxObject> tx = new List<TxObject>();
            FileInfo fi = new FileInfo(path);
            if (!fi.Exists)
            {
                return tx;
                throw new Exception("输入路径不存在");
            }

            string[] paths = path.Split(new char[] { '.' });
            if (paths.Length < 1 || !paths[paths.Length - 1].Equals("tx"))
            {
                return tx;
                throw new Exception("这不是一个rx文件");
            }

            StreamReader sr = new StreamReader(path);
            string s = sr.ReadToEnd();
            sr.Close();
            string[] strseperator = new string[] { "begin_<points>", "begin_<grid>" };
            string[] result = null;
            result = s.Split(strseperator, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < result.Length; i++)
            {
                TxObject temp = new TxObject(result[i]);
                tx.Add(temp);
            }
            return tx;

        }

        //从setup文件中获取发射机的频率
        private static double GetFrequence(TxObject tx, SetupObject setup)
        {
            List<WaveformStp> waveform = setup.waveform.allWaveforms;
            List<AntennaStp> antenna = setup.antenna.allAntennas;
            if (tx.at.waveformNum == -1)
            {
                for (int j = 0; j < antenna.Count; j++)
                {
                    if (tx.at.antennaNum == antenna[j].antenna)
                        tx.at.waveformNum = j;
                }
            }
            int i;
            for (i = 0; i < waveform.Count; i++)
            {
                if (tx.at.waveformNum == waveform[i].waveform)
                    break;
            }
            return waveform[i].CarrierFrequency / 1000000;
        }

        //从setup中获取发射频率带宽
        private static double GetWidth(TxObject tx, SetupObject setup)
        {
            double temp;
            List<WaveformStp> waveform = setup.waveform.allWaveforms;
            int i;
            for (i = 0; i < waveform.Count; i++)
            {
                if (tx.at.waveformNum == waveform[i].waveform)
                    break;
            }
            switch (waveform[i].type)
            {
                case "Sinusoid":
                    temp = waveform[i].bandwidth;
                    break;
                case "RaisedCosine":
                    temp = 10000000*(1+waveform[i].Rolloff)/(waveform[i].PulseWidth/0.0000001);
                    break;
                case "Chirp":
                    temp = waveform[i].StopFrequency - waveform[i].StartFrequency;
                    break;
                case "Blackman":
                    temp = 60000000 / (waveform[i].PulseWidth / 0.0000001);
                    break;
                case "Gaussian":
                    temp = 23190000 / (waveform[i].PulseWidth / 0.0000001);
                    break;
                case "GaussianDerivative":
                    temp =16480000 / (waveform[i].PulseWidth / 0.0000001);;
                    break;
                case "Hamming":
                    temp = 40000000 / (waveform[i].PulseWidth / 0.0000001);;
                    break;
                case "RootRaisedCosine":
                    temp = 10000000 * (1 + waveform[i].Rolloff) / (waveform[i].PulseWidth / 0.0000001);
                    break;
                default:
                    temp = 0;
                    break;
            }
            return temp/1000000;
        }
        #endregion

        //从传入的文件中提取发射机点
        public static List<Node> GetTx(string txpath,string setup,string terpath)
        {
            List<Node> txnode = new List<Node>();
            List<TxObject> txs = TxReader(txpath);
            string ter = ReadTer.GetTer(terpath);      
            Node temp;
            for (int i = 0; i < txs.Count; i++)
            {
                temp = new Node();
                temp.IsEnd = false; temp.IsReceiver = false; temp.LayNum = 1;
                temp.PointStyle = "Tx"; temp.Position = new Point(txs[i].Lc.vertical[0], txs[i].Lc.vertical[1], txs[i].Lc.vertical[2]);
                Intersection.GetZValue(ter, temp.Position); temp.Position.z += txs[i].Lc.vertical[2];
                temp.TxNum = txs[i].num; temp.Power = new Plural(txs[i].at.power); temp.NodeName = txs[i].name;
                //加从setup文件中获取的频率函数、UAN
                temp.frequence = GetFrequence(txs[i], SetupFileProceed.GetSetupFile.GetSetup(setup));
                temp.FrequenceWidth = GetWidth(txs[i], SetupFileProceed.GetSetupFile.GetSetup(setup));
                temp.UAN = ReadUan.GetUan(txs[i], SetupFileProceed.GetSetupFile.GetSetup(setup));
                txnode.Add(temp);
            }
            return txnode;
        }

    }
}
